import Util.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

import static Util.UrlCollect.*;

/*
  根据JSON文件，配置模板大纲
 */
public class TemplateConfiguration {
    static HttpUtil http = new HttpUtil();
    static int templateId;
    static int i=0;
    public static void main(String[] args) throws Exception {

        File folder = new File("C:\\Users\\EDY\\Desktop\\新建文件夹");
        File[] files = folder.listFiles();
        for (File file : files) {
            // 对文件和文件夹进行处理
            // 读取文件内容
            byte[] fileBytes = Files.readAllBytes(Paths.get("C:\\Users\\EDY\\Desktop\\新建文件夹\\" + file.getName()));
            String fileContent = new String(fileBytes);
            String[] strings = file.getName().split("【");

            JSONArray jsonArray = JSONArray.parseArray(fileContent);
            templateId= Integer.parseInt(strings[0]);
            add(jsonArray, 0);
        }



    }

    static void addContent(int id,String s) {
        String data="";
        switch (s){
            case "all":
                data="{\"data\":[{\"unknownId\":\"0\",\"type\":\"UNKNOWN_INTERPRETATION\"},{\"unknownId\":\"0\",\"type\":\"UNKNOWN_INDICATOR_OBJECT\"}],\"outlineId\":" + id + "}";
                break;
            case "text":
                data="{\"data\":[{\"unknownId\":\"0\",\"type\":\"UNKNOWN_INTERPRETATION\"}],\"outlineId\":" + id + "}";
                break;
            case "zhibiao":
                data="{\"data\":[{\"unknownId\":\"0\",\"type\":\"UNKNOWN_INDICATOR_OBJECT\"}],\"outlineId\":" + id + "}";
                break;
        }
        try {
            http.doBodyPost(ADD_BODY_INDICATOR_OBJECT_URL, data);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    static JSONObject addSubtitle(int sourceId, int templateId) {
        String data = "{\"sourceId\":" + sourceId + ",\"templateId\":" + templateId + ",\"type\":\"子标题\"}";
        try {
            return http.doBodyPost(ADD_SUBTITLE_INFORMATION_URL, data);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    static void add(JSONArray jsonArray, int sourceId) throws Exception {
        for (Object o : jsonArray) {
            JSONObject json1 = (JSONObject) o;
            JSONArray children1 = json1.getJSONArray("children");
            JSONObject post;
            JSONObject jsonObject;
            post = addSubtitle(sourceId, templateId);
            int resId = post.getIntValue("data");
            String j = "{\"titleFootnote\":\"" + sub(json1.getString("title")) + "\",\"outlineId\":" + resId + "}";
            jsonObject = http.doBodyPost(MODIFY_TITLE_URL, j);
            if (jsonObject.getIntValue("code")!=0){
                throw new Exception("报错咯");
            }else {
                System.out.println(i++);
            }
            if ((json1.getString("description") != null)) {
                addContent(resId,"text");
            }
            if (children1 != null) {
                add(children1, resId);
            } else {
                if (("chart".equals(json1.getString("type")))||(json1.getString("description") != null)){
                    addContent(resId,"all");
                }else {
                    addContent(resId,"zhibiao");
                }
//                String j1="{\"display\":true,\"outlineId\":"+resId+"}";
//                http.doBodyPost(url1, j1);
            }
        }
    }

    static String sub(String s) {
        String[] segments = s.split(" ");
        return segments[segments.length - 1];
    }
}
